package com.doge.controller.admin;

import com.doge.bean.base.BaseRespVo;
import com.doge.bean.bo.brand.BrandBo;

import com.doge.bean.bo.brand.BrandCreateBo;
import com.doge.bean.bo.brand.BrandDeleteBo;
import com.doge.bean.pojo.brand.Brand;
import com.doge.bean.vo.brand.BrandResultVo;
import com.doge.exception.Param401Exception;
import com.doge.exception.Param402Exception;
import com.doge.exception.Param602Exception;
import com.doge.service.admin.brand.BrandService;
import com.doge.util.anno.MarketHibernateValid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: doge
 * @description: 品牌制造商
 * @author: Mr.Jiang
 * @create: 2021-11-29 23:31
 **/
@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    @RequestMapping("list")
    public BaseRespVo list(BrandBo brandBo, String id, String name) throws Param402Exception {
        Integer idInteger = -1;
        if (id != null && id != "") {
            try {
                idInteger = Integer.parseInt(id);
            } catch (Exception e) {
                throw new Param402Exception("参数值不对");
            }
        }
        BrandResultVo brandResultVo = brandService.list(brandBo, idInteger, name);
        return BaseRespVo.ok(brandResultVo);
    }

    @MarketHibernateValid
    @RequestMapping(value = "create")
    public BaseRespVo create(@Valid @RequestBody BrandCreateBo brandCreateBo, BindingResult bindingResult) throws Exception {
        if (brandCreateBo.getDesc() == null || brandCreateBo.getFloorPrice() == null || brandCreateBo.getName() == null || brandCreateBo.getPicUrl() == null) {
            throw new Param401Exception("请输入完全");
        }
        Brand brand = brandService.create(brandCreateBo);
        return BaseRespVo.ok(brand);
    }

    @MarketHibernateValid
    @RequestMapping("update")
    public BaseRespVo update(@Valid @RequestBody Brand brand, BindingResult bindingResult) throws Param401Exception, Param402Exception {
        if (brand.getDesc() == null || brand.getFloorPrice() == null || brand.getName() == null || brand.getPicUrl() == null) {
            throw new Param401Exception("参数不对");
        }
        Brand brandReturn = brandService.update(brand);
        return BaseRespVo.ok(brandReturn);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody BrandDeleteBo brandDeleteBo) {
        brandService.delete(brandDeleteBo.getId());
        return BaseRespVo.ok();
    }

}
